package com.ws.service.impl;

import com.ws.entity.LoginUser;
import com.ws.repository.LoginUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：ws
 * @date ：Created in 2023/11/6 15:44
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService{
    @Autowired
    private LoginUserRepository loginUserRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LoginUser loginUser = loginUserRepository.findByUsername(username);
        if (loginUser == null) {
            throw new UsernameNotFoundException("LoginUser not found with username: " + username);
        }
        String[] roles = loginUser.getRoles().split(",");

        return User.builder()
                .username(loginUser.getUsername())
                .password(loginUser.getPassword())
                .roles(roles)
                .build();
    }


}
